package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.ApiResponse;
import org.chen.service.impl.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/upload")
@Slf4j
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传模型视频文件
     *
     * @param file     视频文件
     * @param filename 指定的文件名（可选）
     * @return 上传结果
     */
    @PostMapping("/model")
    public ApiResponse<Map<String, String>> uploadModelVideo(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "filename", required = false) String filename) {
        
        try {
            Map<String, String> filePaths = fileUploadService.uploadModelVideo(file, filename);
            return ApiResponse.success(filePaths);
        } catch (IOException e) {
            log.error("上传模型视频失败", e);
            return ApiResponse.error("上传模型视频失败: " + e.getMessage());
        }
    }

    /**
     * 上传音频文件
     *
     * @param file     音频文件
     * @param filename 指定的文件名（可选）
     * @return 上传结果
     */
    @PostMapping("/audio")
    public ApiResponse<String> uploadAudio(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "filename", required = false) String filename) {
        
        try {
            String filePath = fileUploadService.uploadAudio(file, filename);
            return ApiResponse.success(filePath);
        } catch (IOException e) {
            log.error("上传音频文件失败", e);
            return ApiResponse.error("上传音频文件失败: " + e.getMessage());
        }
    }

    /**
     * 健康检查
     * 
     * @return 服务状态
     */
    @GetMapping("/health")
    public ApiResponse<String> health() {
        return ApiResponse.success("服务正常运行");
    }
}